package com.yurun.loginmodule.controller;

import com.github.pagehelper.PageHelper;
import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Employee;
import com.yurun.commonapi.pojo.StudentDocument;
import com.yurun.employeemodule.service.EmployeeService;
import com.yurun.loginmodule.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吼吼权
 * @mail houhouquan@163.com
 * @Date 2021/8/4 9:55 下午
 */
@Api(tags = "员工模块")
@RestController
@RequestMapping("/employeeManage")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "获取机构的某分店所有用户信息")
    @RequestMapping(value = "/allBranchEmployeeInfo")
    @RequiresPermissions("/allBranchEmployeeInfo")
    @ResponseBody
    public CommonResult getBranchEmployeeInfo(ServletRequest request, int pageNum, int pageSize) {

        String token = WebUtils.toHttp(request).getHeader("Authorization");
        String employeeId = JWTUtil.getUsername(token.substring(6));
        Employee employee = employeeService.getEmployeeById(BigInteger.valueOf(Long.parseLong(employeeId)));
        BigInteger organizationId = employee.getOrganizationId();
        BigInteger branchId = employee.getBranchId();
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeService.getBranchEmployeeInfo(organizationId,branchId);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取机构所有用户信息")
    @RequiresPermissions("/allEmployeeInfo")
    @RequestMapping(value = "/allEmployeeInfo")
    @ResponseBody
    public CommonResult getEmployeeInfo(ServletRequest request, String keyword, String branchId, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token.substring(6));
//        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeService.getEmployeeList(keyword, BigInteger.valueOf(Long.parseLong(branchId)), BigInteger.valueOf(Long.parseLong(organizationId)));
//        //分页
//        Map<String, Object> map = new HashMap<>();
//        map.put("total", list.size());
//        map.put("data", zljPageHelper(list, pageNum, pageSize));
//        return CommonResult.success(map);
          return CommonResult.success(list);
    }

    @ApiOperation("获取某个员工信息")
    @RequiresPermissions("/getOneEmployeeInfo")
    @RequestMapping("/getOneEmployeeInfo")
    public CommonResult getOneEmployeeInfo(BigInteger employeeId){
        Employee employee = employeeService.getEmployeeById(employeeId);
        if (employee!=null) {
            return CommonResult.success(employee);
        }
        return CommonResult.failed("对不起，暂无该员工信息！");
    }

    @ApiOperation(value = "逻辑删除用户")
    @RequiresPermissions("/delEmployee")
    @RequestMapping(value = "/delEmployee")
    @ResponseBody
    public CommonResult delEmployee(BigInteger employeeId) {
        return CommonResult.success(employeeService.delEmployeeById(employeeId));
    }

    @ApiOperation(value = "新增员工")
    @RequiresPermissions("/addEmployee")
    @RequestMapping(value = "/addEmployee")
    @ResponseBody
    public CommonResult addEmployee(ServletRequest request, Employee employee) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token.substring(6));
        return employeeService.addEmployee(employee, organizationId);
    }

    @ApiOperation(value = "修改员工")
    @RequiresPermissions("/editEmployee")
    @RequestMapping(value = "/editEmployee")
    @ResponseBody
    public CommonResult editEmployee(Employee employee) {
        return CommonResult.success(employeeService.editEmployee(employee));
    }

    @ApiOperation("重置员工密码")
    @RequiresPermissions("/resetEmployeePwd")
    @RequestMapping("/resetEmployeePwd")
    @ResponseBody
    public CommonResult resetEmployeePwd(BigInteger employeeId) {
        return employeeService.resetEmployeePwd(employeeId);
    }

    @ApiOperation("修改密码")
    @RequestMapping("/editEmployeePwd")
    public CommonResult editEmployeePwd(BigInteger employeeId, String employeePwd, String newEmployeePwd) {
        return employeeService.editEmployeePwd(employeeId, employeePwd, newEmployeePwd);
    }

    @ApiOperation("获取某个班级信息")
    @RequestMapping("/getOneClassroomInfo")
    public CommonResult getOneClassroomInfo(BigInteger classroomId) {
        return employeeService.getOneClassroomInfo(classroomId);
    }
    //分页
    public List<Employee> zljPageHelper(List<Employee> list, int pageNum, int pageSize) {
        List<Employee> temp = new ArrayList<>();

        for (int i = pageSize * (pageNum - 1); i < list.size() && i < (pageSize * (pageNum - 1) + pageSize); i++) {
            temp.add(list.get(i));
        }
        return temp;
    }
}
